package com.ssl.rms.util;

import com.google.common.collect.Maps;
import com.ssl.rms.component.SpringContextHolder;
import com.ssl.rms.entity.Menu;
import com.ssl.rms.entity.UserRole;
import com.ssl.rms.service.MenuService;
import com.ssl.rms.service.UserRoleService;
import com.ssl.rms.shiro.realm.UserRealm.Principal;
import com.ssl.rms.utils.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.UnavailableSecurityManagerException;
import org.apache.shiro.session.InvalidSessionException;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.lang.Nullable;

import java.util.List;
import java.util.Map;

/**
 * 描述：用户工具类
 *
 * @author ssl
 * @create 2018/02/08 9:32
 */
public class UserUtils {
    private static Logger logger = LoggerFactory.getLogger(UserUtils.class);
    private static UserRoleService userRoleService = SpringContextHolder.getBean(UserRoleService.class);
    private static MenuService menuService = SpringContextHolder.getBean(MenuService.class);

    /**
     * 菜单缓存
     */
    public static final String CACHE_MENU_LIST = "menuList";

    /**
     * 获取当前登录用户所用有的菜单树形集合
     *
     * @return
     */
    public static List<Menu> getMenus(String type) {
        List<Menu> menus = (List<Menu>) getSession(CACHE_MENU_LIST);
        if (null == menus) {
            /** 当前登录用户id */
            Long userId = getPrincipal().getId();
            Map<String, Object> map = Maps.newHashMap();
            if (StringUtils.isNotBlank(type)) {
                map.put("type", type);
            }
            /** 是否为root用户 */
            if (!getPrincipal().getRoot()) {
                /** 查询当前用户拥有的菜单 */
                map.put("userId", userId);
            }
            menus = menuService.selectMenus(map);
            menus = MenuTreeUtils.getTreeMenu(menus);
            putSession(CACHE_MENU_LIST, menus);
        }

        return menus;
    }

    /**
     * 获取用户的角色id集合
     *
     * @param userId
     * @return
     */
    private static List<UserRole> getRoles(Long userId) {
        return userRoleService.findRoles(userId);
    }

    /**
     * 获取Session
     *
     * @return
     */
    public static Session getSession() {
        try {
            Subject subject = SecurityUtils.getSubject();
            Session session = subject.getSession();
            return session;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取授权用户信息
     *
     * @return
     */
    public static Principal getPrincipal() {
        try {
            Subject subject = SecurityUtils.getSubject();
            Principal principal = (Principal) subject.getPrincipal();
            if (principal != null) {
                return principal;
            }
        } catch (UnavailableSecurityManagerException e) {

        } catch (InvalidSessionException e) {

        }
        return null;
    }

    public static Object getSession(String key) {
        return getSession(key, null);
    }

    public static Object getSession(String key, Object defaultValue) {
        Object obj = getSession().getAttribute(key);
        return obj == null ? defaultValue : obj;
    }

    public static void putSession(String key, Object value) {
        getSession().setAttribute(key, value);
    }

    public static void removeSession(String key) {
        getSession().removeAttribute(key);
    }

}
